<template>
    <el-card>
        <el-form :inline="true">
            <el-form-item label="一级分类">
                <el-select :disabled="scene == 0 ? false : true" placeholder="请选择" style="width: 18rem;"
                    v-model="categoryStore.c1Id" @change="handler1">
                    <!--:value即为下拉菜单收集到的数据-->
                    <el-option v-for="(c1, index) in categoryStore.c1Arr" :key="c1.id" :label="c1.name"
                        :value="c1.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select :disabled="scene == 0 ? false : true" placeholder="请选择" style="width: 18rem;"
                    v-model="categoryStore.c2Id" @change="handler2">
                    <el-option v-for="(c2, index) in categoryStore.c2Arr" :key="c2.id" :label="c2.name"
                        :value="c2.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select :disabled="scene == 0 ? false : true" placeholder="请选择" style="width: 18rem;"
                    v-model="categoryStore.c3Id">
                    <el-option v-for="(c3, index) in categoryStore.c3Arr" :key="c3.id" :label="c3.name"
                        :value="c3.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
//引入分类相关的仓库
import useCategoryStore from '@/store/modules/category'

//接收父组件传递过来的scene
defineProps(['scene'])

let categoryStore = useCategoryStore()

//通知仓库获取一级分类的方法
const getC1 = () => {
    categoryStore.getC1()
}
//一级分类下拉菜单的change事件,选中项发生变化时会触发
const handler1 = () => {
    //需要将二级分类与三级分类的数据清空
    categoryStore.c2Id = ''
    categoryStore.c3Id = ''
    categoryStore.c2Arr = []
    //通知仓库获取二级分类数组
    categoryStore.getC2()
}
//二级分类下拉菜单的change事件,选中项发生变化时会触发
const handler2 = () => {
    //需要将三级分类的数据清空
    categoryStore.c3Id = ''
    //通知仓库获取二级分类数组
    categoryStore.getC3()
}
//分类全局组件挂载完毕，通知仓库发请求获取一级分类的数据
onMounted(() => {
    getC1()
})
</script>

<style scoped lang="scss"></style>